package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnWorkOrderStoreRichemontBo;
import com.ruoyi.system.domain.vo.KfnWorkOrderStoreRichemontVo;
import com.ruoyi.system.domain.KfnWorkOrderStoreRichemont;
import com.ruoyi.system.mapper.KfnWorkOrderStoreRichemontMapper;
import com.ruoyi.system.service.IKfnWorkOrderStoreRichemontService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 历峰店铺Service业务层处理
 *
 * @author Panda
 * @date 2024-01-05
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderStoreRichemontServiceImpl implements IKfnWorkOrderStoreRichemontService {

    private final KfnWorkOrderStoreRichemontMapper baseMapper;

    /**
     * 查询历峰店铺
     */
    @Override
    public KfnWorkOrderStoreRichemontVo queryById(Long storeId){
        return baseMapper.selectVoById(storeId);
    }

    /**
     * 查询历峰店铺列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderStoreRichemontVo> queryPageList(KfnWorkOrderStoreRichemontBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrderStoreRichemont> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderStoreRichemontVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询历峰店铺列表
     */
    @Override
    public List<KfnWorkOrderStoreRichemontVo> queryList(KfnWorkOrderStoreRichemontBo bo) {
        LambdaQueryWrapper<KfnWorkOrderStoreRichemont> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnWorkOrderStoreRichemont> buildQueryWrapper(KfnWorkOrderStoreRichemontBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrderStoreRichemont> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStoreNumber()), KfnWorkOrderStoreRichemont::getStoreNumber, bo.getStoreNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), KfnWorkOrderStoreRichemont::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), KfnWorkOrderStoreRichemont::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistricts()), KfnWorkOrderStoreRichemont::getDistricts, bo.getDistricts());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), KfnWorkOrderStoreRichemont::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getManageArea()), KfnWorkOrderStoreRichemont::getManageArea, bo.getManageArea());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), KfnWorkOrderStoreRichemont::getStoreName, bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), KfnWorkOrderStoreRichemont::getStoreType, bo.getStoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreBrand()), KfnWorkOrderStoreRichemont::getStoreBrand, bo.getStoreBrand());
        lqw.eq(bo.getOpenDate() != null, KfnWorkOrderStoreRichemont::getOpenDate, bo.getOpenDate());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreContacts()), KfnWorkOrderStoreRichemont::getStoreContacts, bo.getStoreContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreContactNumber()), KfnWorkOrderStoreRichemont::getStoreContactNumber, bo.getStoreContactNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreEmail()), KfnWorkOrderStoreRichemont::getStoreEmail, bo.getStoreEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreStatus()), KfnWorkOrderStoreRichemont::getStoreStatus, bo.getStoreStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreGps()), KfnWorkOrderStoreRichemont::getStoreGps, bo.getStoreGps());
        return lqw;
    }

    /**
     * 新增历峰店铺
     */
    @Override
    public Boolean insertByBo(KfnWorkOrderStoreRichemontBo bo) {
        KfnWorkOrderStoreRichemont add = BeanUtil.toBean(bo, KfnWorkOrderStoreRichemont.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStoreId(add.getStoreId());
        }
        return flag;
    }

    /**
     * 修改历峰店铺
     */
    @Override
    public Boolean updateByBo(KfnWorkOrderStoreRichemontBo bo) {
        KfnWorkOrderStoreRichemont update = BeanUtil.toBean(bo, KfnWorkOrderStoreRichemont.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrderStoreRichemont entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除历峰店铺
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
